/*
 * @描述: 用户管理数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { querySysUser, queryUsername, deleteSysUser, addSysUser, updateSysUser, exportSysUser, querySysOrgTree, queryRoleName } from '@/services/api';

export default {
  namespace: 'sysUser',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    orgTreeData: {
      list: []
    },
    roleSelectData: [],
    usernameSelectData: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysUser, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchOrgTree({ payload }, { call, put }) {
      const response = yield call(querySysOrgTree, payload);
      yield put({
        type: 'saveOrgTree',
        payload: response
      });
    },
    *fetchRole({ payload }, { call, put }) {
      const response = yield call(queryRoleName, payload);
      yield put({
        type: 'saveRole',
        payload: response
      });
    },
    *fetchUsername({ payload }, { call, put }) {
      const response = yield call(queryUsername, payload);
      yield put({
        type: 'saveUsername',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addSysUser, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteSysUser, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateSysUser, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *export({ payload }, { call, put }) {
      const response = yield call(exportSysUser, payload);
      yield put({
        type: 'save',
        payload: response
      });
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveOrgTree(state, action) {
      return {
        ...state,
        orgTreeData: action.payload.data
      };
    },
    saveRole(state, action) {
      return {
        ...state,
        roleSelectData: action.payload.data
      };
    },
    saveUsername(state, action) {
      return {
        ...state,
        usernameSelectData: action.payload.data
      };
    }
  }
};
